<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;


/**
 * vue商城
 *商品管理
 * @icon fa fa-user
 */
class Product extends Backend
{

    protected $relationSearch = true;
    protected $Typemodel = null;
    protected $Unitmodel = null;
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Product.Product');
        $this->Unitmodel = model('Product.Unit');
        $this->Typemodel = model('Product.Type');
        $unit = $this->Unitmodel->order('id', 'desc')->select();
        $type = $this->Typemodel->order('weigh', 'desc')->select();
        $unitdata = [];
        $typedata = [];
        foreach ($unit as $item) {
            $unitdata[$item['id']] = $item['name'];
        }
        foreach ($type as $item) {
            $typedata[$item['id']] = $item['name'];
        }
        $rentstatusdata = [
            '1' => '普通商品',
            '2' => '租赁商品只能租',
            '3' => '既购买也租赁的商品'
        ];
        $flagdata = [
            '1' => '新品',
            '2' => '热销',
            '3' => '推荐'
        ];
        $this->assign(['unit' => $unitdata, 'type' => $typedata, 'rentstatus' => $rentstatusdata, 'flag' => $flagdata]);
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->with(['type', 'unit'])
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->with(['type', 'unit'])
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            if (!empty($params)) {
                $this->error("参数不能为空");
            }
            $params = $this->request->post('row/a');
            $res['code'] = 1;
            if (!empty($params['thumbs'])) {
              
                if (!uploading($params['thumbs'])) {
                    $this->error("上传图片到后台失败");
                }
            }
            $result = $this->model->validate('common/Product/Product')->save($params);
            if (!$result) {
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('当前商品不存在'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['id'] = $ids;
            if (empty($params)) {
                $this->error("参数不能为空");
            }
          
            $result = $this->model->validate('common/Product/Product')->isUpdate()->save($params);
            if ($result === false) {
                // 判断图片是否有上传
                if (!empty($params['thumbs']) && $params['thumb'] != $row['thumbs']) {
                    $imgs = explode(',', $params['thumbs']);
                    $rowimgs = explode(',', $row['thumbs']);
                    $delimg = array_diff($imgs, $rowimgs);
                    foreach ($delimg as $img) {
                        is_file(ltrim($img, '/')) && unlink(ltrim($img, '/'));
                    }
                }
                $this->error($this->model->getError());
            }
            // 判断图片是否有上传
            if (!empty($params['thumbs']) && $params['thumbs'] != $row['thumbs']) {
                $imgs = explode(',', $params['thumbs']);
                $rowimgs = explode(',', $row['thumbs']);
                $addimgs=array_diff($imgs, $rowimgs);
                $delimgs=array_diff($rowimgs,$imgs);
                if(count($addimgs)>0){
                    $addimgs=implode(',',$addimgs);
                    if (!uploading($addimgs)) {
                        $this->error("上传图片到后台失败");
                    }
                }
                if(count($delimgs)>0){
                    foreach ($delimgs as $img) {
                        is_file(ltrim($img, '/')) && unlink(ltrim($img, '/'));
                        delImg($img);
                    }
                }

               
            }
            $this->success();
        }

        $this->assign('row', $row);
        return $this->view->fetch();
    }
    /**
     *软删除
     */
    public function del($ids = null)
    {
        $List = $this->model->all($ids);
        if (empty($List)) {
            $this->error('当前商品不存在');
        }
        $result = $this->model->destroy($ids);
        if (!$result) {
            $this->error($this->model->getError());
        }
       
        $this->success();
    }
    // 分类列表
    public function type()
    {
        $type = $this->Typemodel->order('weigh', 'desc')->select();
        $data = [];
        foreach ($type as $item) {
            $data[$item['name']] = $item['name'];
        }
        echo json_encode($data);
    }
    // 商品单位列表
    public function unit()
    {
        $unit = $this->Unitmodel->order('id', 'desc')->select();
        $data = [];
        foreach ($unit as $item) {
            $data[$item['name']] = $item['name'];
        }
        echo json_encode($data);
    }
    public function toggle($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('当前商品不存在'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $status = ltrim($params['params'], 'status=');
            $data = [
                'id' => $ids,
                'status' => $status
            ];
            $result = $this->model->isUpdate()->save($data);
            if ($result === false) {
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
}
